#define CRT_SECURE_NO_WARNINGS 1

#include"Queue.h"


void QueueInit(Queue* s)
{
	assert(s);

	s->head = s->tail = NULL;
	s->size = 0;
}

void QueuePush(Queue* s, QueueDataType x)
{
	assert(s);

	QN* newnode = (QN*)malloc(sizeof(QN));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	newnode->next = NULL;
	newnode->data = x;
	if (s->tail == NULL)
	{
		s->head = s->tail = newnode;
	}
	else
	{
		s->tail->next = newnode;
		s->tail = newnode;
	}
	s->size++;
}


void QueuePop(Queue* s)
{
	assert(s);
	assert(s->head);

	if (s->head->next == NULL)
	{
		s->head = s->tail = NULL;
	}
	else
	{
		QN* next = s->head->next;

		free(s->head);
		s->head = next;
	}
	s->size--;
}


bool QueueEmpty(Queue* s)
{
	assert(s);

	return s->head == NULL;
}

int QueueSize(Queue* s)
{
	assert(s);
	return s->size;
}


QueueDataType QueueFront(Queue* s)
{
	assert(s);

	return s->head->data;
}

QueueDataType QueueBack(Queue* s)
{
	assert(s);

	return s->tail->data;
}


void QueueDestory(Queue* s)
{
	assert(s);
	QN* cur = s->head;
	while (cur)
	{
		QN* next = cur->next;
		free(cur);
		cur = next;
	}
	s->head = s->tail = NULL;
	s->size = 0;
}